package controllers;

import play.*;
import play.mvc.*;
import play.data.validation.*;
import play.libs.Images;

import java.util.*;

import models.*;

public class Application extends Controller {

	@Before
	static void addDefaults() {
	    renderArgs.put("blogTitle", Play.configuration.getProperty("blog.title"));
	    renderArgs.put("blogBaseline", Play.configuration.getProperty("blog.baseline"));
	}

    public static void index() {
        Post frontPost = Post.find("order by postedAt desc").first();
        List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10);
        render(frontPost, olderPosts);
    }

    public static void show(Long id) {
    	Post post = Post.findById(id);
    	render(post);
    }

    /**
     * @author kelianglaing
     * 在一个post下添加评论
     * @param
     * @param
     * @param
     */
    public static void postComment(Long postId, @Required String author, @Required String content) {
	   	Post post = Post.findById(postId); 
	   	//错误验证
	   	if (validation.hasErrors()) {
        	render("Application/show.html", post);
    	}
	   	post.addComment(author, content); 
	   	//flash可以传递信息从一个action到另一个action
	   	flash.success("Thanks for posting %s", author);
	   	show(postId); 
   	}

   	public static void captcha() {
    	Images.Captcha captcha = Images.captcha();
    	renderBinary(captcha);
	}

    //以标签为检索条件
    public static void listTagged(String tag) {
        List<Post> posts = Post.findTaggedWith(tag);
        render(tag, posts);
    }
   
 }